package com.ruicar.afs.cloud.apply.pre.loan.controller;

import com.ruicar.afs.cloud.apply.common.entity.ApplyRemindDetails;
import com.ruicar.afs.cloud.apply.pre.loan.condition.RemindDetailsCondition;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyRemindDetailsService;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyReportService;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.parameter.commom.enums.WhetherEnum;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @description: 案件留言
 * @author: sijun.yu
 * @date: 2020/5/18 21:33
 */
@Slf4j
@RestController
@RequestMapping(value = "remind")
@AllArgsConstructor
public class RemindDetailsController {

    private ApplyReportService reportService;
    private ApplyRemindDetailsService remindDetailsService;

    /**
     * 查询留言列表
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/getRemindList")
    @ApiOperation("查询留言列表")
    public IResponse getRemindDetailsList(@ModelAttribute RemindDetailsCondition condition) {

        List<ApplyRemindDetails> list = reportService.getRemindDetailsList(condition);
        return IResponse.success(list);
    }

    /**
     * 保存留言信息
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/savaRemindDetails")
    @ApiOperation("保存留言信息")
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "apply_remind_details", allEntries = true)
    public IResponse savaRemindDetails(@RequestBody RemindDetailsCondition condition) {

        ApplyRemindDetails remindDetails = new ApplyRemindDetails();
        remindDetails.setApplyNo(condition.getApplyNo());
        remindDetails.setCreateTime(new Date());
        remindDetails.setMsgContent(condition.getMsgContent());
        remindDetails.setCreateBy(SecurityUtils.getUsername());
        remindDetails.setIsSelf(WhetherEnum.YES.getIndex());
        remindDetailsService.save(remindDetails);

        return IResponse.success(Boolean.TRUE).setMsg("保存成功");
    }

    /**
     * 撤回留言
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/deleteRemindDetails")
    @ApiOperation("撤回留言")
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "apply_remind_details", allEntries = true)
    public IResponse deleteRemindDetails(@RequestBody RemindDetailsCondition condition) {

        ApplyRemindDetails remindDetails = remindDetailsService.getById(condition.getId());
        if (remindDetails == null) {
            return IResponse.fail("记录不存在");
        }
        remindDetailsService.removeById(condition.getId());
        return IResponse.success(Boolean.TRUE).setMsg("删除成功");
    }

}